package com.digitalchina.lxnsy.activity.old;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessEvaluate;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.EvaluateProductVO;
import com.digitalchina.lxnsy.entity.EvaluateSellerVO;
import com.digitalchina.lxnsy.entity.OrderListVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：白彦东
 * 创建时间：2017/1/18 14:15
 * 修改备注：
 * ================================================
 */

public class EvaluateDetailsActivity extends BaseActivity {

    @Bind(R.id.evaluate_tv_merchants_name)
    TextView evaluateTvMerchantsName;
    @Bind(R.id.merchant_iv_star1)
    ImageView merchantIvStar1;
    @Bind(R.id.merchant_iv_star2)
    ImageView merchantIvStar2;
    @Bind(R.id.merchant_iv_star3)
    ImageView merchantIvStar3;
    @Bind(R.id.merchant_iv_star4)
    ImageView merchantIvStar4;
    @Bind(R.id.merchant_iv_star5)
    ImageView merchantIvStar5;
    @Bind(R.id.service_iv_star1)
    ImageView serviceIvStar1;
    @Bind(R.id.service_iv_star2)
    ImageView serviceIvStar2;
    @Bind(R.id.service_iv_star3)
    ImageView serviceIvStar3;
    @Bind(R.id.service_iv_star4)
    ImageView serviceIvStar4;
    @Bind(R.id.service_iv_star5)
    ImageView serviceIvStar5;
    @Bind(R.id.evaluation_product_item_layout)
    LinearLayout evaluationProductItemLayout;

    private OrderListVO orderListVO; // 订单
    private int score = 1; // 商品满意度评分
    private int accordStore = 1;// 符合度评分
    private int serviceScore = 1;// 服务态度评分
    private String isAnonymity = "1";// 是否匿名 1 是 0否
    private List<Integer> scoreList = new ArrayList<>();
    private List<TextView> tvTextsList = new ArrayList<>();
    private int goodsListSize;
    private int msgWhat;
    private List<EvaluateProductVO> evaluateProductVO = new ArrayList<>();

    private ImageView ivStar1;
    private ImageView ivStar2;
    private ImageView ivStar3;
    private ImageView ivStar4;
    private ImageView ivStar5;
    private TextView tvBtnAddEvaluate;
    private TextView tvEvaluateContent;
    private TextView tvAddEvaluateContent;

    @Override
    public void setLayout() {
        setTitle("评价详情");
        setContentLayout(R.layout.activity_evaluate_details);
    }

    @Override
    public void initMember() {
        if (getIntent().hasExtra("OrderListVO")) {
            orderListVO = (OrderListVO) getIntent().getSerializableExtra("OrderListVO");
            BusinessEvaluate.getProductEvaluateByOrderNo(this, orderListVO.getOrderNo(), mHandler);
            BusinessEvaluate.getSellerScoreByOrderNo(this, orderListVO.getOrderNo(), orderListVO.getSellerId(), mHandler);
        }
    }

    /**
     * 设置内容
     */
    private void intContent() {
//        setMerchantStar();
    }

    /**
     * 初始化商品评价视图
     *
     * @param productNum 商品数量
     */
    Map<String, Object> dataMap = new HashMap<>(); // 传递下个页面参数

    private void initProductEvaluateView(int productNum) {
        for (int i = 0; i < productNum; i++) {

            scoreList.add(i, score);

            View productEvaluation = LayoutInflater.from(this).inflate(R.layout.item_evaluate_product_details, null);

            TextView tvTitle = (TextView) productEvaluation.findViewById(R.id.item_evaluate_details_tv_product_title);
            TextView tvIntroduce = (TextView) productEvaluation.findViewById(R.id.item_evaluate_details_tv_product_introduce);
            SimpleDraweeView ivPicture = (SimpleDraweeView) productEvaluation.findViewById(R.id.item_evaluate_product_iv_images);
            ivStar1 = (ImageView) productEvaluation.findViewById(R.id.product_iv_star1);
            ivStar2 = (ImageView) productEvaluation.findViewById(R.id.product_iv_star2);
            ivStar3 = (ImageView) productEvaluation.findViewById(R.id.product_iv_star3);
            ivStar4 = (ImageView) productEvaluation.findViewById(R.id.product_iv_star4);
            ivStar5 = (ImageView) productEvaluation.findViewById(R.id.product_iv_star5);
            tvBtnAddEvaluate = (TextView) productEvaluation.findViewById(R.id.item_evaluate_tv_additional_evaluation);
            tvEvaluateContent = (TextView) productEvaluation.findViewById(R.id.item_evaluate_product_content);
            tvAddEvaluateContent = (TextView) productEvaluation.findViewById(R.id.item_evaluate_product_add_evaluate);
            tvTextsList.add(tvBtnAddEvaluate); // TODO 可能无用
            FrescoUtil.showImageSmall(orderListVO.getOrderItems().get(i).getProductImg(), ivPicture);//商品图片
            tvTitle.setText(orderListVO.getOrderItems().get(i).getProductName() != null ? orderListVO.getOrderItems().get(i).getProductName() : "");
            tvIntroduce.setText(orderListVO.getOrderItems().get(i).getProductDesc() != null ? orderListVO.getOrderItems().get(i).getProductDesc() : "");
            setProductStar(evaluateProductVO.get(i).getScore());
            tvEvaluateContent.setText(evaluateProductVO.get(i).getContent());
            dataMap.put("evaluateProductId", evaluateProductVO.get(i).getId());
            dataMap.put("productImg", orderListVO.getOrderItems().get(i).getProductImg() != null ? orderListVO.getOrderItems().get(i).getProductImg() : "");
            dataMap.put("evaluateContent", orderListVO.getOrderItems().get(i).getProductName() != null ? orderListVO.getOrderItems().get(i).getProductName() : "");
            dataMap.put("evaluateScore", evaluateProductVO.get(i).getScore());
            if (!StringUtil.isStrEmpty(evaluateProductVO.get(i).getAdditionContent())) {
                tvAddEvaluateContent.setVisibility(View.VISIBLE);
                tvBtnAddEvaluate.setVisibility(View.INVISIBLE);
                tvAddEvaluateContent.setText(String.format("追加评价: %s", evaluateProductVO.get(i).getAdditionContent()));
            } else {
                tvBtnAddEvaluate.setVisibility(View.VISIBLE);
            }

            final int position = i;

            tvBtnAddEvaluate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    GotoUtil.gotoActivity(EvaluateDetailsActivity.this, AdditionalEvaluationActivity.class, "data", dataMap);
                }
            });

            evaluationProductItemLayout.addView(productEvaluation);

        }
    }

    /**
     * 设置商品评价星数
     *
     * @param pos 存储位置
     * @param num 存储星数
     */
    private void setStar(int pos, int num) {
        score = num;
        scoreList.set(pos, score);
        setProductStar(score);
    }

    /**
     * 设置商品评论星数
     *
     * @param score
     */
    private void setProductStar(int score) {
        switch (score) {
            case 1:
                ivStar1.setImageResource(R.drawable.icon_average_star_solid);
                ivStar2.setImageResource(R.drawable.icon_average_star_hollow);
                ivStar3.setImageResource(R.drawable.icon_average_star_hollow);
                ivStar4.setImageResource(R.drawable.icon_average_star_hollow);
                ivStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 2:
                ivStar1.setImageResource(R.drawable.icon_average_star_solid);
                ivStar2.setImageResource(R.drawable.icon_average_star_solid);
                ivStar3.setImageResource(R.drawable.icon_average_star_hollow);
                ivStar4.setImageResource(R.drawable.icon_average_star_hollow);
                ivStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 3:
                ivStar1.setImageResource(R.drawable.icon_average_star_solid);
                ivStar2.setImageResource(R.drawable.icon_average_star_solid);
                ivStar3.setImageResource(R.drawable.icon_average_star_solid);
                ivStar4.setImageResource(R.drawable.icon_average_star_hollow);
                ivStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 4:
                ivStar1.setImageResource(R.drawable.icon_average_star_solid);
                ivStar2.setImageResource(R.drawable.icon_average_star_solid);
                ivStar3.setImageResource(R.drawable.icon_average_star_solid);
                ivStar4.setImageResource(R.drawable.icon_average_star_solid);
                ivStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 5:
                ivStar1.setImageResource(R.drawable.icon_average_star_solid);
                ivStar2.setImageResource(R.drawable.icon_average_star_solid);
                ivStar3.setImageResource(R.drawable.icon_average_star_solid);
                ivStar4.setImageResource(R.drawable.icon_average_star_solid);
                ivStar5.setImageResource(R.drawable.icon_average_star_solid);
                break;
        }
    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                msgWhat = msg.what;
                switch (msg.what) {
                    case MSG.MSG_GET_PRODUCT_EVALUATE_BY_ORDER_NO_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_PRODUCT_EVALUATE_BY_ORDER_NO_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_GET_SELLER_SCORE_BY_ORDER_NO_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_SELLER_SCORE_BY_ORDER_NO_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        switch (msgWhat) {
            case MSG.MSG_GET_PRODUCT_EVALUATE_BY_ORDER_NO_SUCCESS:
                evaluateProductVO = FastJsonUtil.getListBean(object.toString(), "body", "getProductEvaluateByOrderNo", EvaluateProductVO.class);
                initProductEvaluateView(evaluateProductVO.size());
                break;
            case MSG.MSG_GET_SELLER_SCORE_BY_ORDER_NO_SUCCESS:
                EvaluateSellerVO evaluateSellerVO = FastJsonUtil.getBean(object.toString(), "body", "evaluateSeller", EvaluateSellerVO.class);
                if (evaluateSellerVO != null) {
                    setMerchantStar(evaluateSellerVO.getAccordStore());
                    setServiceStar(evaluateSellerVO.getServiceScore());
                }
                break;
        }

    }

    @Override
    public void rightOnClick() {

    }

    /**
     * 设置商品符合度评分
     *
     * @param score
     */
    private void setMerchantStar(int score) {
        accordStore = score;
        switch (score) {
            case 1:
                merchantIvStar1.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar2.setImageResource(R.drawable.icon_average_star_hollow);
                merchantIvStar3.setImageResource(R.drawable.icon_average_star_hollow);
                merchantIvStar4.setImageResource(R.drawable.icon_average_star_hollow);
                merchantIvStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 2:
                merchantIvStar1.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar2.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar3.setImageResource(R.drawable.icon_average_star_hollow);
                merchantIvStar4.setImageResource(R.drawable.icon_average_star_hollow);
                merchantIvStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 3:
                merchantIvStar1.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar2.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar3.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar4.setImageResource(R.drawable.icon_average_star_hollow);
                merchantIvStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 4:
                merchantIvStar1.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar2.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar3.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar4.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 5:
                merchantIvStar1.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar2.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar3.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar4.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar5.setImageResource(R.drawable.icon_average_star_solid);
                break;
        }
    }

    /**
     * 设置商户服务态度星数
     *
     * @param score
     */
    private void setServiceStar(int score) {
        serviceScore = score;
        switch (score) {
            case 1:
                serviceIvStar1.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar2.setImageResource(R.drawable.icon_average_star_hollow);
                serviceIvStar3.setImageResource(R.drawable.icon_average_star_hollow);
                serviceIvStar4.setImageResource(R.drawable.icon_average_star_hollow);
                serviceIvStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 2:
                serviceIvStar1.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar2.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar3.setImageResource(R.drawable.icon_average_star_hollow);
                serviceIvStar4.setImageResource(R.drawable.icon_average_star_hollow);
                serviceIvStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 3:
                serviceIvStar1.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar2.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar3.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar4.setImageResource(R.drawable.icon_average_star_hollow);
                serviceIvStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 4:
                serviceIvStar1.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar2.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar3.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar4.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 5:
                serviceIvStar1.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar2.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar3.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar4.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar5.setImageResource(R.drawable.icon_average_star_solid);
                break;
        }
    }
}
